package com.android.dopewars;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class HighScores extends Activity {
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.high_scores);
        /*
        Bundle extras = getIntent().getExtras();
        if (extras == null) {
        	extras = new Bundle();
        }
        */
        View focus_view = null;
        for (int i = 0; i < Global.high_score_list_scores_.size(); ++i) {
        	String name = i < Global.high_score_list_names_.size() ? Global.high_score_list_names_.elementAt(i) : "none";
        	String score = Global.high_score_list_scores_.elementAt(i);
        	String date = i < Global.high_score_list_dates_.size() ? Global.high_score_list_dates_.elementAt(i) : "";
        	String comment = i < Global.high_score_list_comments_.size() ? Global.high_score_list_comments_.elementAt(i) : "";
        	View next_view = newHighScoreView(name, score, date, comment, i == Global.high_score_list_row_to_highlight_);
        	if (i == Global.high_score_list_row_to_highlight_) {
        		focus_view = next_view;
       	 	}
        	((LinearLayout)findViewById(R.id.high_score_layout)).addView(next_view);
        }
        if (focus_view != null) {
        	((ScrollView)findViewById(R.id.high_score_scroller)).requestChildFocus(focus_view, focus_view);
        }
        ((Button)findViewById(R.id.ok_button)).setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		Intent i = new Intent();
        		setResult(RESULT_OK, i);
        		finish();
        	}
        });
        
        WindowManager.LayoutParams dialog_params = getWindow().getAttributes();
    	dialog_params.width = WindowManager.LayoutParams.FILL_PARENT;
    	getWindow().setAttributes(dialog_params);
    }
    
    private LinearLayout newHighScoreView(String player_name, String score, Boolean highlight) {
    	return newHighScoreView(player_name, score, null, null, highlight);
    }
    
    private LinearLayout newHighScoreView(String player_name, String score, String date, String comment, Boolean highlight) {
    	LinearLayout m = new LinearLayout(this);
    	m.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    	m.setOrientation(LinearLayout.VERTICAL);
    	
    	LinearLayout l = new LinearLayout(this);
    	l.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    	l.setOrientation(LinearLayout.HORIZONTAL);
    	if (highlight) {
    		l.setBackgroundColor(Color.rgb(255, 255, 200));
    	}
    	TextView name = new TextView(this);
    	name.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    	name.setTextSize((float)16.0);
    	name.setText(player_name);
    	name.setSingleLine();
    	if (highlight) {
    		name.setTextColor(Color.rgb(0,0,0));
    	} else {
    		name.setTextColor(Color.rgb(255, 255, 255));
    	}
    	l.addView(name);
    	TextView score_view = new TextView(this);
    	score_view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    	score_view.setTextSize((float)16.0);
    	score_view.setSingleLine();
    	if (highlight) {
    		score_view.setTextColor(Color.rgb(20, 80, 20));
    	} else {
    		score_view.setTextColor(Color.rgb(80, 240, 80));
    	}
    	score_view.setText("$" + score);
    	score_view.setGravity(Gravity.RIGHT);
    	l.addView(score_view);
    	
    	m.addView(l);
    	
    	if (date != null || comment != null) {
	    	LinearLayout l2 = new LinearLayout(this);
	    	l2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
	    	l2.setOrientation(LinearLayout.HORIZONTAL);
	    	if (highlight) {
	    		l2.setBackgroundColor(Color.rgb(255, 255, 200));
	    	}
	    	TextView score_date = new TextView(this);
	    	score_date.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
	    	score_date.setTextSize((float)10.0);
	    	score_date.setText(date);
	    	score_date.setSingleLine();
	    	if (highlight) {
	    		score_date.setTextColor(Color.rgb(60, 60, 20));
	    	} else {
	    		score_date.setTextColor(Color.rgb(240, 240, 180));
	    	}
	    	l2.addView(score_date);
	    	TextView comment_view = new TextView(this);
	    	comment_view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
	    	comment_view.setTextSize((float)10.0);
	    	comment_view.setText(comment);
	    	comment_view.setSingleLine();
	    	if (highlight) {
	    		comment_view.setTextColor(Color.rgb(20, 20, 60));
	    	} else {
	    		comment_view.setTextColor(Color.rgb(180, 180, 255));
	    	}
	    	comment_view.setGravity(Gravity.RIGHT);
	    	l2.addView(comment_view);
	    	
	    	m.addView(l2);
    	}
    	return m;
    }
}
